
import AddModal from '@/components/Modal/AddModal';
import MedicineDetail from '@/components/MedicineDetail';
import InOutHistory from '@/components/InOutHistory';
import Delivery from '@/components/Delivery';
import { AddForm, ExportRest } from './form'
import { useRef, useImperativeHandle, forwardRef, useState } from 'react'

export enum ModalType {
    DIALOG = 'dialog',
    DETAIL = 'detail',
    DELIVERY = 'delivery',
    PUTIN = 'putin',
    HISTORY = 'history',
    EXPORT = 'export'
}


const Modals = ({ onAddFinish, onInFinish, actionRef }, ref) => {
    const dialogRef = useRef<any>() // 新建药品
    const detailRef = useRef<any>() // 查看详情
    const deliveryRef = useRef<any>() // 出库
    const putInRef = useRef<any>() // 入库
    const historyRef = useRef<any>() // 出入库统计
    const exportRef = useRef<any>() // 结存导出
    const [name, setName] = useState('')

    const showModal = (type, name = '') => {
        switch (type) {
            case ModalType.DIALOG:
                dialogRef?.current?.showModelRef(); break;
            case ModalType.DETAIL:
                setName(name);
                detailRef?.current?.showModelRef(); break;
            case ModalType.DELIVERY:
                setName(name);
                deliveryRef?.current?.showModelRef(); break;
            case ModalType.PUTIN:
                putInRef?.current?.showModelRef();
                setName(name)
                break;
            case ModalType.HISTORY:
                setName(name)
                historyRef?.current?.showModelRef(); break;
            case ModalType.EXPORT:
                exportRef?.current?.showModelRef(); break;
        }
    }

    const hideModal = (type) => {
        switch (type) {
            case ModalType.DIALOG:
                dialogRef?.current?.hideModelRef(); break;
            case ModalType.DETAIL:
                detailRef?.current?.hideModelRef(); break;
            case ModalType.DELIVERY:
                deliveryRef?.current?.hideModelRef(); break;
            case ModalType.PUTIN:
                putInRef?.current?.hideModelRef(); break;
            case ModalType.HISTORY:
                historyRef?.current?.hideModelRef(); break;
            case ModalType.EXPORT:
                exportRef?.current?.hideModelRef(); break;
        }
    }

    useImperativeHandle(ref, () => ({
        show: showModal,
        hide: hideModal
    }))

    return (<>
        <AddModal ref={dialogRef} title='新建药品'>
            <AddForm onFinish={onAddFinish} readonly={false} />
        </AddModal>

        <AddModal ref={detailRef} title='药品详情' width='80%' style={{ top: '20' }}>
            <MedicineDetail name={name} />
        </AddModal>

        <AddModal ref={deliveryRef} title='药品出库' width='80%' style={{ top: '20' }}>
            <Delivery name={name} actionRef={actionRef} />
        </AddModal>

        <AddModal ref={putInRef} title='药品入库'>
            <AddForm onFinish={onInFinish} readonly={true} name={name} />
        </AddModal>

        <AddModal ref={historyRef} title='出入库记录' width='80%' style={{ top: '20' }}>
            <InOutHistory name={name} />
        </AddModal>

        <AddModal ref={exportRef} title='结存导出' >
            <ExportRest onExportFinish={() => hideModal(ModalType.EXPORT)} />
        </AddModal>
    </>)
}

export default forwardRef(Modals)